package edu.gatech.arktos;

import com.google.gdata.client.spreadsheet.CellQuery;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;

public class Worksheet {
	int width;
	int height;
	String entries[][];
	
	public Worksheet(WorksheetEntry entry, SpreadsheetService service){
		width = entry.getColCount();
		height = entry.getRowCount();
		
		entries = new String[width][height];
		
		for(int x = 0; x < width; x++){
			for (int y = 0; y < height; y++){
				entries[x][y] = "[EMPTY CELL]";
			}
		}
		
		CellQuery query = new CellQuery(entry.getCellFeedUrl());
		try {
			CellFeed feed = service.query(query, CellFeed.class);
			for(CellEntry c : feed.getEntries()){
				entries[c.getCell().getCol() - 1][c.getCell().getRow() - 1] = c.getCell().getValue();
			}
		} catch (Exception ex) {
			System.out.println("FUCK CONSTRUCTOR PROBLEMS");
		}
	}
	
	public int GetWidth(){
		return width;
	}
	
	public int GetHeight(){
		return height;
	}
	
	public String GetValue(int x, int y){
		if (x >= width || y >= height || x < 0 || y < 0){
			return "OUTSIDE BOUNDS";
		}
		return entries[x][y];
	}
	
	public String GetValueRC(int y, int x){
		return GetValue(x, y);
	}
	
	public int GetInt(int x, int y){
		if (x >= width || y >= height || x < 0 || y < 0){
			return -999;
		}
		try {
			return Integer.parseInt(entries[x][y]);
		} catch (Exception ex){
			return -999;
		}
	}
	
	public int GetIntRC(int y, int x){
		return GetInt(x, y);
	}
}
